package com.itheima.reggie.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.reggie.common.BaseContext;
import com.itheima.reggie.common.Result;
import com.itheima.reggie.dto.OrdersDto;
import com.itheima.reggie.pojo.OrderDetail;
import com.itheima.reggie.pojo.Orders;
import com.itheima.reggie.service.OrderDetailService;
import com.itheima.reggie.service.OrdersService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.stream.Collectors;


/**
 * @author gakki
 * @create 2022/5/4 17:06
 */
@Slf4j
@RestController
@RequestMapping("/order")
public class OrdersController {

    @Autowired
    private OrdersService ordersService;

    @Autowired
    private OrderDetailService orderDetailService;

    /**
     * 订单提交
     *
     * @param orders
     * @return
     */
    @PostMapping("/submit")
    public Result submitOrder(@RequestBody Orders orders) {
        log.info("用户下单orders:{}", orders);
        ordersService.submitOrder(orders);
        return Result.success("下单成功！");
    }

    /**
     * 订单查询
     *
     * @param page
     * @param pageSize
     * @param number
     * @param beginTime
     * @param endTime
     * @return
     */
    @GetMapping("/page")
    public Result<Page<Orders>> queryByPage(Integer page, Integer pageSize, String number, String beginTime, String endTime) {
        log.info("订单查询，page：{}，pageSize：{}，number：{}，beginTime：{}，endTime：{}", page, pageSize, number, beginTime, endTime);
        Page<Orders> pageInfo = new Page(page, pageSize);
        LambdaQueryWrapper<Orders> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(number != null, Orders::getNumber, number);
        lambdaQueryWrapper.between(beginTime != null && endTime != null, Orders::getOrderTime, beginTime, endTime);
        ordersService.page(pageInfo, lambdaQueryWrapper);
        return Result.success(pageInfo);
    }

    /**
     * 更新订单状态
     *
     * @param orders
     * @return
     */
    @PutMapping
    public Result<String> modifyOrderStatus(@RequestBody Orders orders) {
        log.info("更新订单状态，orders：{}", orders);
        LambdaUpdateWrapper<Orders> lambdaUpdateWrapper = new LambdaUpdateWrapper<>();
        lambdaUpdateWrapper.eq(Orders::getId, orders.getId());
        lambdaUpdateWrapper.set(Orders::getStatus, orders.getStatus());
        ordersService.update(lambdaUpdateWrapper);
        return Result.success("订单状态更新成功！");
    }

    /**
     * 订单查询
     *
     * @param page
     * @param pageSize
     * @return
     */
    @GetMapping("/userPage")
    public Result<Page> queryByPageOnPhone(Integer page, Integer pageSize) {
        log.info("订单查询，page：{}，pageSize：{}", page, pageSize);
        //根据用户id查询order
        Page<Orders> ordersPage = new Page(page, pageSize);
        Page<OrdersDto> ordersDtoPagea = new Page<>();
        LambdaQueryWrapper<Orders> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(Orders::getUserId, BaseContext.getCurrentId()).orderByDesc(Orders::getOrderTime);
        ordersService.page(ordersPage, lambdaQueryWrapper);

        //将查询的内容进行拷贝
        BeanUtils.copyProperties(ordersPage, ordersDtoPagea, "records");

        //根据order中的订单号，查询对应的orderDetail，并封装成集合
        List<Orders> ordersList = ordersPage.getRecords();

        List<OrdersDto> ordersDtoList = ordersList.stream().map((item) -> {
            OrdersDto ordersDto = new OrdersDto();
            BeanUtils.copyProperties(item, ordersDto);
            LambdaQueryWrapper<OrderDetail> orderDetailLambdaQueryWrapper = new LambdaQueryWrapper<>();
            orderDetailLambdaQueryWrapper.eq(OrderDetail::getOrderId, item.getId());
            List<OrderDetail> orderDetailList = orderDetailService.list(orderDetailLambdaQueryWrapper);
            ordersDto.setOrderDetails(orderDetailList);
            return ordersDto;
        }).collect(Collectors.toList());

        ordersDtoPagea.setRecords(ordersDtoList);

        return Result.success(ordersDtoPagea);
    }

    /**
     * 再来一单
     *
     * @param orders
     * @return
     */
    @PostMapping("/again")
    public Result<String> oneMoreOrder(@RequestBody Orders orders) {
        log.info("再下一单，id:{}", orders.getId());
        ordersService.oneMoreOrder(orders);
        return Result.success("成功再下一单！");
    }

}
